"""Fixer that changes unicode to str, unichr to chr, and u"..." into "...".
Based on lib2to3
"""

import re
from lib2to3.pgen2 import token
from lib2to3 import fixer_base

class FixStrToUnicode(fixer_base.BaseFix):

    PATTERN = "STRING | NAME<'str'>"

    def transform(self, node, results):
        if node.type == token.NAME:
            if node.value == "str":
                new = node.clone()
                new.value = "unicode"
                return new
            # XXX Warn when __unicode__ found?
        elif node.type == token.STRING:
            if re.match(r"[\'\"]", node.value):
                new = node.clone()
                new.value = 'u%s' % new.value
                return new
